package com.cskaoyan.shiro;

import com.cskaoyan.bean.User;
import com.cskaoyan.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author tangjian
 * @Date 2021/10/21 16:27
 * @description
 */
@Component
public class WxUserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        User user = userService.selectPwdByUserName(username);
        if (user == null){
            user = new User();
            user.setUsername(username);
        }
        return new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        //User user = (User) principalCollection.getPrimaryPrincipal();
        //暂时没有权限可以授予 只有等级
        //用户的授权
        return null;
    }
}
